package com.tank.action.itemsObjct;

import java.io.File;
import java.util.List;


import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tank.bean.User;
import com.tank.bean.item.Image;
import com.tank.bean.item.Product;
import com.tank.bean.product.ParentType;
import com.tank.service.ImageService;
import com.tank.service.ItemsManageService;
import com.tank.util.SaveImage;
import com.tank.util.TankUtil;

@Controller
public class ItemsObjectAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private List<ParentType> parentTypeList;
	@Autowired
	private ItemsManageService itemsManageService;
	@Autowired
	private ImageService imageService;
	private List<Image> images;
	private List<Image> imagesItem;
	private String itemTag;
	private Product product;
	private User user;
	private Integer productId;
	@Override
	public String execute() throws Exception {
		parentTypeList = itemsManageService.queryItemsType();
		return "success";
	}
	
	public String uploadItemsInfo() throws Exception{
		String readlPath = ServletActionContext.getServletContext().getRealPath("\\imageTag");
		String readlPathForUpload = ServletActionContext.getServletContext().getRealPath("\\upload");
		File tagFile = null;
		File upFile = null; 
		String fileName = "";
		SaveImage saveImage = new SaveImage();
		user = TankUtil.getCurrentUser();
		try{
			images = imageService.imageForPreview(itemTag);
			if(images.size()!=0){
				for(int i=0;i<images.size();i++){
					fileName=images.get(i).getName();
					tagFile = new File(readlPath+"\\"+fileName);
					upFile = new File(readlPathForUpload+"\\"+fileName);
					saveImage.copyImage(tagFile,upFile);
					images.get(i).setItem(product);
				}
				product.setImages(images);
				product.setCover(images.get(0));
				product.setAuthor(user);
			}
			itemsManageService.uploadItems(product);
			return "submitItem";
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		parentTypeList = itemsManageService.queryItemsType();
		return "success";
	}
	
	public String modifyItem(){
		parentTypeList = itemsManageService.queryItemsType();
		product = itemsManageService.querySingleItems(productId);
		return "modifyItem";
	}
	public String modifyItemForSubmit(){
		String readlPath = ServletActionContext.getServletContext().getRealPath("\\imageTag");
		String readlPathForUpload = ServletActionContext.getServletContext().getRealPath("\\upload");
		File tagFile = null;
		File upFile = null; 
		String fileName = "";
		SaveImage saveImage = new SaveImage();
		user = TankUtil.getCurrentUser();
		try{
			images = imageService.imageForPreview(itemTag);
			if(images.size()!=0){
				for(int i=0;i<images.size();i++){
					fileName=images.get(i).getName();
					tagFile = new File(readlPath+"\\"+fileName);
					upFile = new File(readlPathForUpload+"\\"+fileName);
					saveImage.copyImage(tagFile,upFile);
					images.get(i).setItem(product);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		imagesItem=imageService.queryImagesById(productId);
		for(Image image:imagesItem){
			images.add(image);
		}
		product.setId(productId);
		product.setCover(images.get(0));
		product.setImages(images);
		product.setAuthor(user);
		itemsManageService.updateItem(product);
		return "submitItem";
	}
	
	public List<ParentType> getParentTypeList() {
		return parentTypeList;
	}
	public void setParentTypeList(List<ParentType> parentTypeList) {
		this.parentTypeList = parentTypeList;
	}
	public String getItemTag() {
		return itemTag;
	}
	public void setItemTag(String itemTag) {
		this.itemTag = itemTag;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Image> getImages() {
		return images;
	}

	public void setImages(List<Image> images) {
		this.images = images;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public List<Image> getImagesItem() {
		return imagesItem;
	}

	public void setImagesItem(List<Image> imagesItem) {
		this.imagesItem = imagesItem;
	}
	
}
